package com.park.platform.service.impl;

import com.park.common.bean.ActivityInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.common.poo.ActivityInfo;
import com.park.common.util.FileUtil;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IActivityService;
import com.park.platform.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    活动
 */
@Service
public class ActivityServiceImpl extends BaseService implements IActivityService {

    @Autowired
    private IBaseDao baseDao;

    @Autowired
    private IOperatorService operatorService;

    @Override
    public PageBean getActivitys(ActivityInputView activityInputView){
        StringBuilder headSql = new StringBuilder("SELECT *");
        StringBuilder bodySql = new StringBuilder(" FROM activity_info ai");
        if(activityInputView.isCollector()){
            bodySql.append(" JOIN user_collector uc ON(uc.serviceId = ai.activityId AND uc.id = :id)");
        }
        StringBuilder whereSql = new StringBuilder(" WHERE ai.activityStatus = ").append(IDBConstant.LOGIC_STATUS_YES);
        whereSql.append(" ORDER BY ai.activitySeq DESC, ai.createTime DESC");

        PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, activityInputView);
        List<Map<String, Object>> list = pageBean.getList();
        for(Map<String, Object> map : list){
            map.put("activityPic", FileUtil.getImgHttp(StrUtil.objToStr(map.get("activityPic"))));
        }

        return pageBean;
    }

    @Override
    public Map<String, Object> getActivityDetails(int activityId, Integer uid){
        Map<String, Object> resultMap = new HashMap<>();
        ActivityInfo activityInfo = baseDao.getToEvict(ActivityInfo.class, activityId);
        activityInfo.setActivityPic(FileUtil.getImgHttp(activityInfo.getActivityPic()));
        resultMap.put("activityInfo", activityInfo);
        if(uid != null) {
            resultMap.put("collector", operatorService.getUserCollector(IDBConstant.LOGIC_STATUS_YES, activityId, uid));
        }
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> getRandActivitys(Integer count){
        StringBuilder sql = new StringBuilder("SELECT activityId, activityName, activityTitle, activityPrice, activityPic FROM activity_info");
        sql.append(" WHERE activityStatus = ").append(IDBConstant.LOGIC_STATUS_YES);
        sql.append(" ORDER BY RAND() LIMIT 0,").append(count != null ? count : 4);
        List<Map<String, Object>> list = baseDao.queryBySql(sql.toString());
        for(Map<String, Object> map : list){
            map.put("activityPic", FileUtil.getImgHttp(StrUtil.objToStr(map.get("activityPic"))));
        }
        return list;
    }

    @Override
    public ActivityInfo getActivityInfo(int activityId){
        ActivityInfo activityInfo = baseDao.getToEvict(ActivityInfo.class, activityId);
        activityInfo.setActivityPic(FileUtil.getImgHttp(activityInfo.getActivityPic()));
        return activityInfo;
    }

}
